<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      <div>form:{{ form }}</div>
      <el-form :model="form">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            ref="tree"
            :data="menuList"
            show-checkbox
            node-key="id"
            :props="{ children: 'children', label: 'title' }"
          >
          </el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="edit">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {formCancel} from "../../../mixins/mixins"
import {
  
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../http/api";

import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  mixins:[formCancel],
  data() {
    return {
      // 角色列表
      menuList: [],

      // 5.1 初始化表单数据
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },
  mounted() {
    // 一进来就取角色的数据
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.menuList = res.data.list ? res.data.list : [];
      }
    }); 
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),
    
    // 清空表单数据
    empty() {
      // 清空form
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
      // 清空树上的选中的节点
      this.$refs.tree.setCheckedKeys([])
    },
    // 5.4点了添加
    add() {
      // ①将树上的数据赋值给form.menus

      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      // ②将form发送给后端
      reqroleadd(this.form).then((res) => {
        if (res.data.code === 200) {
          // ①弹成功
          successAlert(res.data.msg);

          // ②清除表单数据
          this.empty();
          // ③关闭弹框
          this.info.isshow = false;
          // ④刷新列表
          this.$emit("init");
        }
      });
    },
    
    // 7.4编辑
    getOne(id) {
      reqroleinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          // 补id ,因为修改的时候需要id
          this.form.id = id;
          // 给树赋值
          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus))
        }
      });
    },
    // 8.1修改
    edit() {
    // ① 先将树上的数据给form.menus 
    this.form.menus=JSON.stringify(this.$refs.tree.getCheckedKeys())
    // ② 发修改的请求
      reqroleedit(this.form).then((res) => {
        if (res.data.code === 200) {
          // ①弹成功
          successAlert(res.data.msg);


          // 如果修改的id和当前用户的roleid一致，那么应该清除用户信息，并退出登录
          if(this.form.id==this.userInfo.roleid){
            // 清除用户信息
            this.changeUserInfo({})
            // 退出登录
            this.$router.push("/login")
            return;
          }


          // ②清除表单数据
          this.empty();
          // ③关闭弹框
          this.info.isshow = false;
          // ④刷新列表
          this.$emit("init");
        }
      });
    },
   
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>